﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MessageHandlingServer.Server2
{
    class MyServer2 : IServer
    {
        Action<string> _writer;
        decimal _sum;
        object _lock = new object();

        public MyServer2(Action<string> writer)
        {
            _writer = writer;
        }

        public bool ProcessMessage(IMessage message)
        {
            try
            {
                lock (_lock)
                {
                    Thread.Sleep(200);
                    _sum += message.GetValue();
                    _writer(String.Format("Original message: {0} Sum = {1}\n", message.ToText(), _sum));
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
